$(function () {

    // let u_name = localStorage.getItem("username");
    // console.log(typeof u_name);
    // let res = u_name.split("").splice(3,6,"******").join("");
    // let r = u_name.replace(res,"******");
    // console.log(r);
    // $(".please_replace_login").text(r);
    // $(".please_register").css({
    //     display:"none"
    // })  ;  
    
    let shop_data = JSON.parse(localStorage.getItem("cartData"));
    let html = "";
    for (let attr in shop_data) {
        console.log(shop_data[attr]);
        html += `   <div class="col_check">
                        <input type="checkbox" class="choose_this" name="" id="">
                    </div>
                    <div class="col_goods_msg">
                        <div class="item_pic">
                            <a href="javascript:void(0)">
                                <img src="${shop_data[attr].img_url}"
                                    alt="">
                            </a>
                        </div>
                        <div class="item_name">
                            <a href="javascript:void(0)">
                                ${shop_data[attr].goods_name}
                            </a>
                        </div>
                    </div>
                    <div class="col_specs">
                        <div class="specs_line">
                            <p>配件：【手柄握把】马力欧收藏版</p>
                        </div>
                    </div>
                    <div class="col_price">
                        <div class="price_line">
                            <span>
                                <i>￥</i>
                                <em>${shop_data[attr].new_price}</em>
                            </span>
                        </div>
                    </div>
                    <div class="col_amount">
                        <div class="item_amount">
                            <button class="amount_reduction">-</button>
                            <input type="text" class="amount_inp" value="${shop_data[attr].count}">
                            <button class="amount_add">+</button>
                        </div>
                    </div>
                    <div class="col_sum">
                        <p class="shop_price">
                            <i>￥</i>
                            <span>${shop_data[attr].new_price * shop_data[attr].count}</span>
                        </p>
                    </div>
                    <div class="col_del">
                        <a href="javascript:void(0)">删除</a>
                    </div>`
    }
    $(".pur_info_main").html(html)

    // 购物车 增加数量功能 
    $(".pur_info_main").on("click", ".amount_add", function () {
        let shop_count = Number($(".amount_inp").val());
        shop_count += 1;
        $(".amount_inp").attr("value", shop_count);
        sum_count();
        sumPrice();
    })

    // 购物车 减少数量功能
    $(".pur_info_main").on("click", ".amount_reduction", function () {
        let inp_val = $(".amount_inp").val();
        if (inp_val === "1") {
            $(".amount_reduction").attr("disabled", "disabled");
            $(".amount_reduction").css({
                cursor: "not-allowed"
            })
            sum_count();
            sumPrice();
            return false;
        } else {
            inp_val -= 1;
            $(".amount_inp").attr("value", inp_val);
            sum_count();
            sumPrice();
        }
    })

    // 小计
    function sum_count() {
        $(".shop_price span").text(Number($(".amount_inp").val()) * Number($(".price_line span em").text()));
    }

    // 删除
    $(".pur_info_main").on("click", ".col_del a", function () {
        $(this).parent().parent().remove();
        sumPrice();
        closeAccount();

    })

    // 已选商品件数
    $(".num_color").text($(".pur_info_main").length);

    // 总价
    // - 遍历所有的小计 每次点击增加 减少按钮的时候 调用函数
    function sumPrice() {
        let sum = 0;
        for (let i = 0; i < $(".pur_info_main").length; i++) {
            sum += Number($(".shop_price span").text());

        }
        console.log(sum);
        $(".cart-sum-price b em").text(sum)
    }
    sumPrice();

    // 结算按钮高量
    function closeAccount() {
        if($(".cart-sum-price b em").text() !== "0.00"){
            $(".pay_for a").addClass("active");
        }else{
            $(".pay_for a").removeClass("active");
        }
    }
    closeAccount();
})

// 店铺全选按钮
let i = 0 ; 
$(".choose_my_store").on("change",function(){
    if( i === 0 ){
        console.log($(".choose_this"));
         $(".col_check .choose_this").prop("checked",true);
            i = 1;
    }else{
        $(".col_check .choose_this").prop("checked",false);
        i = 0;
    }

})


// 整体全选 / 反选 方法
let j = 0 ; 

function changeCheckBox(){
    if( j === 0 ){
        $(".col_check .choose_this").prop("checked",true);
        $(".choose_my_store").prop("checked" , true);
        j = 1 ;
    }else if(j === 1){
        $(".col_check .choose_this").prop("checked",false);
        $(".choose_my_store").prop("checked" , false);
        j = 0 ;
    }
}

$(".cart_choose_all").on("change",function(){
  changeCheckBox();
})
$(".del_check").on("change",function(){
    changeCheckBox();
})

